<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :md="6" :sm="8">
            <a-form-item label="名称">
              <a-input placeholder="请输入名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="标识key">
              <a-input placeholder="请输入标识key" v-model="queryParam.key"></a-input>
            </a-form-item>
          </a-col>
          <!--<a-col :md="6" :sm="8">
            <a-form-item label="name">
              <a-input placeholder="请输入name" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
        <template v-if="toggleSearchStatus">
        <a-col :md="6" :sm="8">
            <a-form-item label="key">
              <a-input placeholder="请输入key" v-model="queryParam.key"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="category">
              <a-input placeholder="请输入category" v-model="queryParam.category"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="createTime">
              <a-input placeholder="请输入createTime" v-model="queryParam.createTime"></a-input>
            </a-form-item>
          </a-col>
          </template>-->
          <a-col :md="6" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!--<a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>-->
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!--<div  v-if="addModel">
    <iframe  :src="modelSrc" style="width: 100%;height: 500px" >
    </iframe>
    </div>-->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <!--<a-button type="primary" icon="download" @click="handleExportXls('模型管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>-->


      <!--<a-dropdown v-if="selectedRowKeys.length > 0">-->
        <!--<a-menu slot="overlay">-->
          <!--<a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>-->
        <!--</a-menu>-->
        <!--<a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>-->
      <!--</a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <a-dropdown v-if="this.selectedRowKeys.length>0">

      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      </a-dropdown>


      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">

          <a @click="editModel(record)">模板</a>
          <a-divider type="vertical" />

          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />

          <a @click="handleCopy(record)">复制</a>

          <a-divider type="vertical" />
          <a @click="deployModel(record)">发布</a>

          <a-divider type="vertical" />
          <!--<a @click="handleDelete(record.id)">删除</a>-->
          <a @click="exportXml(record)">导出XML</a>
          <a-divider type="vertical" />

          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>


          <!-- <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <model-modal ref="modalForm" @ok="modalFormOk"></model-modal>
    <!--<model-edit-modal ref="modalEditForm"></model-edit-modal>-->
  </a-card>
</template>

<script>
  import Vue from 'vue'
  import ModelModal from './modules/ModelModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { getAction,deleteAction,putAction,postAction} from '@/api/manage'
  import { SOURCE_NAME } from '@/store/mutation-types'
  import { REDIS_INDEX } from '@/store/mutation-types'
  import moment from "moment"
  export default {
    name: "ModelList",
    mixins:[JeecgListMixin],
    components: {
      ModelModal,
      /*ModelEditModal*/
    },
    data () {
      return {
        description: '模型管理管理页面',
        // 表头
        columns: [
          // {
          //   title: '#',
          //   dataIndex: '',
          //   key:'rowIndex',
          //   width:60,
          //   align:"center",
          //   customRender:function (t,r,index) {
          //     return parseInt(index)+1;
          //   }
          //  },

		   /*{
            title: '模型ID',
            align:"center",
            dataIndex: 'id'
           },*/
          {
            title: '名称',
            align:"center",
            dataIndex: 'name'
          },
		   {
            title: '标识',
            align:"center",
            dataIndex: 'key'
           },
          {
            title: '版本',
            align:"center",
            dataIndex: 'version'
          },
          {
            title: '创建时间',
            align:"center",
            dataIndex: 'createTime',
            customRender: (text, row, index) => {
              if (text != ''){
                return moment(text).format("YYYY-MM-DD HH:mm");
              }
            },
            sorter: true
          },
          {
            title: '更新时间',
            align:"center",
            dataIndex: 'lastUpdateTime',
            customRender: (text, row, index) => {
              if (text != ''){
                return moment(text).format("YYYY-MM-DD HH:mm");
              }
            },
            sorter: true
          },
		   /*{
            title: '模型分类',
            align:"center",
            dataIndex: 'category'
           },*/

          /*{
            title: '模型描述',
            align:"center",
            dataIndex: 'description'
          },*/
		   /*{
            title: 'createTime',
            align:"center",
            dataIndex: 'createTime'
           },*/
		   /*{
            title: 'lastUpdateTime',
            align:"center",
            dataIndex: 'lastUpdateTime'
           },
		   {
            title: 'version',
            align:"center",
            dataIndex: 'version'
           },
		   {
            title: 'metaInfo',
            align:"center",
            dataIndex: 'metaInfo'
           },
		   {
            title: 'deploymentId',
            align:"center",
            dataIndex: 'deploymentId'
           },
		   {
            title: 'editorSourceValueId',
            align:"center",
            dataIndex: 'editorSourceValueId'
           },
		   {
            title: 'editorSourceExtraValueId',
            align:"center",
            dataIndex: 'editorSourceExtraValueId'
           },
		   {
            title: 'tenantId',
            align:"center",
            dataIndex: 'tenantId'
           },*/
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
		url: {
          list: "/act/model/list",
          copy: "/act/model/copy",
          delete: "/act/model/delete",
          deleteBatch: "/act/model/deleteBatch",
          exportXlsUrl: "model/model/exportXls",
          importExcelUrl: "model/model/importExcel",
       },
        addModel:true,
        modelSrc:'',
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
    methods: {
      /**
       * @description 复制工作流模板
       * @param record 选择的对象
       */
      handleCopy(record){
        this.loading=true
        getAction(this.url.copy, { id: record.id }).then(res => {
          if (res.success) {
            this.$message.success(res.message);
            this.searchQuery()
          }else{
            this.$message.error(res.message);
          }
          this.loading=false
        })
      },
      editModel(row){
        /*this.modelSrc=window._CONFIG['domianURL']+'/modeler.html?modelId='+row.id;
        this.addModel=true;*/
        // let domain = window.location.href
        // domain = domain.split(".")[0]
        // domain = domain.split("//")[1]

        // Vue.ls.remove(SOURCE_NAME)
        // Vue.ls.remove(REDIS_INDEX)


        // getAction('/domain/tpDataSourceDomain/queryByDomain', { domain: domain }).then(res => {
        //   if (res.success) {
            let url =process.env.VUE_APP_URL+'/modeler.html?modelId='+row.id

            if(Vue.ls.get(SOURCE_NAME) && Vue.ls.get(REDIS_INDEX)){
              // Vue.ls.remove(SOURCE_NAME)
              // Vue.ls.set(SOURCE_NAME, res.result.sourceName, 7 * 24 * 60 * 60 * 1000)
              // // Vue.ls.remove(REDIS_INDEX)
              // Vue.ls.set(REDIS_INDEX, res.result.redisIndex, 7 * 24 * 60 * 60 * 1000)

              url += '&sourceName='+Vue.ls.get(SOURCE_NAME)
              +'&redisIndex='+Vue.ls.get(REDIS_INDEX);
            }
            // this.$router.push('login')

            window.open(url);
        //   }
        // })




      },
      deployModel(row){
        const that = this;

        this.$confirm({
          title: "提示",
          content: "请确认点击了左上角的保存按钮?",
          onOk: function () {
            that.loading = true;
            getAction("/act/model/deploy/",{id:row.id}).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
              }else{
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });


      },

      exportXml(record){
        //const that = this;

        let url = window._CONFIG['domianURL'] + '/act/model/export?id='+record.id+'&type=xml';
        window.open(url);
        //window.location.href = url;
        // getAction("/act/model/export",{id:row.id}).then((res)=>{
        //   if(res.success){
        //     that.$message.success(res.message);
        //   }else{
        //     that.$message.warning(res.message)
        //   }
        // })
      },

      // handleClickDownloadFile(id) {
      //   let { path } = this.uploadValues[id] || {}
      //   if (path) {
      //     let url = window._CONFIG['staticDomainURL'] + '/' + path
      //     window.open(url)
      //   }
      // },


    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>